(function (vc) {
    vc.extends({
        propTypes: {
            emitChooseProduct: vc.propTypes.string,
            emitLoadData: vc.propTypes.string
        },
        data: {
            chooseProductAndSpecInfo: {
                products: [],
                productCategorys:[],
                prodNameLike: '',
                barCode: '',
                shId: '',
                categoryId:''
            }
        },
        _initMethod: function () {
        },
        _initEvent: function () {
            vc.on('chooseProductAndSpec', 'openChooseProductModel', function (_param) {
                $('#chooseProductAndSpecModel').modal('show');
                $that._refreshChooseProductInfo();
                if (_param.hasOwnProperty('shId')) {
                    $that.chooseProductAndSpecInfo.shId = _param.shId
                }
                $that._listChooseProductCategorys();
                $that._loadAllProductInfo(1, 10);
            });
            vc.on('chooseProductAndSpec', 'setProducts', function (_param) {
                $('#chooseProductAndSpecModel').modal('show');
                $that._refreshChooseProductInfo();
                $that.chooseProductAndSpecInfo.products = _param;
            });
            vc.on('chooseProductAndSpec', 'paginationPlus', 'page_event',
                function (_currentPage) {
                    $that._loadAllProductInfo(_currentPage, 10);
                });
        },
        methods: {
            _loadAllProductInfo: function (_page, _row) {
                let param = {
                    params: {
                        page: _page,
                        row: _row,
                        shopId: vc.getCurrentCommunity().shopId,
                        shId: $that.chooseProductAndSpecInfo.shId,
                        prodNameLike:$that.chooseProductAndSpecInfo.prodNameLike,
                        barCode:$that.chooseProductAndSpecInfo.barCode,
                        categoryId:$that.chooseProductAndSpecInfo.categoryId,
                    }
                };
                //发送get请求
                vc.http.apiGet('/product.queryProductAndSpec',
                    param,
                    function (json) {
                        let _json = JSON.parse(json);
                        $that.chooseProductAndSpecInfo.products = _json.data;
                        vc.emit('chooseProductAndSpec', 'paginationPlus', 'init', {
                            total: _json.records,
                            dataCount: _json.total,
                            currentPage: _page
                        });
                    }, function () {
                        console.log('请求失败处理');
                    }
                );
            },
            chooseProductAndSpec: function (_product) {
                if (_product.hasOwnProperty('name')) {
                    _product.productName = _product.name;
                }
                vc.emit($props.emitChooseProduct, 'chooseProductAndSpec', _product);
                vc.emit($props.emitLoadData, 'listProductData', {
                    productId: _product.productId
                });
                $('#chooseProductAndSpecModel').modal('hide');
            },
            queryProducts: function () {
                $that._loadAllProductInfo(1, 10);
            },
            _refreshChooseProductInfo: function () {
                $that.chooseProductAndSpecInfo = {
                    products: [],
                    productCategorys:[],
                    prodNameLike: '',
                    barCode: '',
                    shId: '',
                    categoryId:''
                }
            },
            _listChooseProductCategorys: function (_page, _rows) {
                let param = {
                    params: {
                        page: 1,
                        row: 50,
                        shopId: vc.getCurrentCommunity().shopId
                    }
                };
                //发送get请求
                vc.http.apiGet('/productCategory.queryProductCategory',
                    param,
                    function (json, res) {
                        let _json = JSON.parse(json);
                        $that.chooseProductAndSpecInfo.productCategorys = _json.data;
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
        }
    });
})(window.vc);
